package com.hotleproject.controller;

import com.hotleproject.pojo.Order;
import com.hotleproject.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/users")
public class OrderController {
    @Autowired
    private OrderService orderService;


    @RequestMapping("/findOrderPage")

    public String findOrderPage(HttpServletRequest request, Model model) throws IOException {
        String spPage = request.getParameter("pageNumber");
        //设置每页条数
        int pageSize = 15;
        //页数
        int pageNo = 0;
        if (spPage == null) {
            pageNo = 1;
        } else {
            pageNo = Integer.valueOf(spPage);
            if (pageNo < 1) {
                pageNo = 1;
            }
        }
        //设置最大页数
        int totalCount = 0;
        int count = orderService.getCount();
        if (count > 0) {
            totalCount = count;
        }
        int maxPage = totalCount % pageSize == 0 ? 1 : totalCount / pageSize + 1;
        if (pageNo > maxPage) {
            pageNo = maxPage;
        }
        int tempPageNo = (pageNo - 1) * pageSize;
        List<Order> list = this.orderService.pageList(tempPageNo, pageSize);

        //最后把信息放入model转发到页面把信息带过去
        model.addAttribute("list", list);
//        model.addAttribute("list",list);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("maxPage", maxPage);
        return "orders_data";
    }

    @RequestMapping("/findOrderByUser")
    public String findOrderByUser(HttpServletRequest request, Model model, HttpSession session) throws IOException {
        String spPage = request.getParameter("pageNumber");
        if(session.getAttribute("users")==null)
            return "please";
        String username = session.getAttribute("users").toString();


        //设置每页条数
        int pageSize = 5;
        //页数
        int pageNo = 0;
        if (spPage == null) {
            pageNo = 1;
        } else {
            pageNo = Integer.valueOf(spPage);
            if (pageNo < 1) {
                pageNo = 1;
            }
        }
        //设置最大页数
        int totalCount = 0;
        int count = orderService.getCount();
        if (count > 0) {
            totalCount = count;
        }
        int maxPage = totalCount % pageSize == 0 ? 1 : totalCount / pageSize + 1;
        if (pageNo > maxPage) {
            pageNo = maxPage;
        }
        int tempPageNo = (pageNo - 1) * pageSize;
        //计算总数量
        //分页查询
//        Map map=new HashMap();
//        map.put("pageNo",tempPageNo);
//        map.put("pageSize",pageSize);
//        map.put("phone",phone);
        List<Order> list = this.orderService.findOrderByUser(username, tempPageNo, pageSize);
        //最后把信息放入model转发到页面把信息带过去
        model.addAttribute("list", list);
//        model.addAttribute("list",list);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("maxPage", maxPage);
        return "HotleOrder";
    }

    //酒店管理员订单信息处理
    @RequestMapping("/findSingleOrderPage")

    public String findSingleOrderPage(HttpSession session, HttpServletRequest request, Model model) throws IOException {
        String spPage = request.getParameter("pageNumber");
        //设置每页条数
        int pageSize = 15;
        //页数
        int pageNo = 0;
        if (spPage == null) {
            pageNo = 1;
        } else {
            pageNo = Integer.valueOf(spPage);
            if (pageNo < 1) {
                pageNo = 1;
            }
        }
        //设置最大页数
        int totalCount = 0;
        int count = orderService.getSingleHotleCount();
        if (count > 0) {
            totalCount = count;
        }
        int maxPage = totalCount % pageSize == 0 ? 1 : totalCount / pageSize + 1;
        if (pageNo > maxPage) {
            pageNo = maxPage;
        }
        int tempPageNo = (pageNo - 1) * pageSize;

        //计算总数量
        //分页查询
//        Map map=new HashMap();
//        map.put("pageNo",tempPageNo);
//        map.put("pageSize",pageSize);
//        map.put("phone",phone);
        String hotlename = session.getAttribute("adminis").toString();
        model.addAttribute("hotlename", hotlename);
//        model.addAttribute("pageNo1", pageNo);
        model.addAttribute("pageNo", tempPageNo);
        model.addAttribute("pageSize", pageSize);
//        return "text";
        List<Order> list=this.orderService.SingleHotlepageList(hotlename,tempPageNo,pageSize);

        //最后把信息放入model转发到页面把信息带过去
        model.addAttribute("list",list);
//        model.addAttribute("list",list);
        model.addAttribute("pageNo",pageNo);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("maxPage",maxPage);
        return "orders_data";
    }


}
